package code.netty.nio.zerocopy;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;

/**
 * @author rocky
 */
public class NewIOClient {

    public static void main(String[] args) throws IOException {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.connect(new InetSocketAddress("127.0.0.1",7001));
        String fileName = "protoc-3.6.1-win32.zip";
        // 得到一个文件filechannel
        FileChannel fileChannel = new FileInputStream(fileName).getChannel();
        // 准备发送
        long startTime = System.currentTimeMillis();

        // 在linux 下一个transferTo 方法就可以完成传输
        // 在Windows 下一次调用 transferTo 只能发送 8m, 就需要分段传输文件,而且要主要传输的位置
        long transferToCount = fileChannel.transferTo(0, fileChannel.size(), socketChannel);
        System.out.println("发送的总的字节数 = " + transferToCount + " 耗时:" + (System.currentTimeMillis() - startTime));
        // 关闭
        fileChannel.close();
    }
}
